package com.gf.help.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gf.base.controller.BasicController;
import com.gf.base.type.Role;
import com.gf.common.bean.DataRequest;
import com.gf.common.bean.DataResponse;
import com.gf.common.bean.Page;
import com.gf.common.constant.Constants;
import com.gf.common.constant.ViewPaths;
import com.gf.common.staticer.MessageRemainsHelper;
import com.gf.common.util.WebHelper;
import com.gf.help.model.Question;
import com.gf.help.model.Reply;
import com.gf.help.service.QuestionService;
import com.gf.help.service.ReplyService;
import com.gf.message.service.MessageService;
import com.gf.user.model.User;

@Controller
@RequestMapping("/help")
public class HelpController extends BasicController {
	private static final Log logger = LogFactory.getLog(HelpController.class);
	@Autowired
	private QuestionService questionService;
	@Autowired
	private ReplyService replyService;

	@Autowired
	private MessageService messageService;

	/**
	 * 登录帮助首页
	 * 
	 * @return ModelAndView
	 */
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public ModelAndView index() {
		logger.info("登录帮助页面");
		ModelAndView mvc = new ModelAndView();

		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user != null) {
			int messageCount = MessageRemainsHelper.getRemains(user.getId());
			mvc.addObject("messageCount", messageCount);
		}
		mvc.addObject("user", user);
		mvc.setViewName(ViewPaths.HELP_VIEW);

		return mvc;
	}

	/**
	 * 提问页面
	 * 
	 * @return ModelAndView
	 */
	@RequestMapping(value = "/askQuestion", method = RequestMethod.GET)
	public ModelAndView askQuestion() {
		logger.info("登录帮助页面");
		User user = WebHelper.getSessionUser();

		ModelAndView mvc = new ModelAndView();
		if (user != null) {
			int messageCount = MessageRemainsHelper.getRemains(user.getId());
			mvc.addObject("messageCount", messageCount);
		}
		mvc.addObject("user", user);
		mvc.setViewName(ViewPaths.HELP_ASKQUESTION);
		return mvc;
	}

	/**
	 * 问题详情
	 * 
	 * @return ModelAndView
	 */
	@RequestMapping(value = "/questionDetail", method = RequestMethod.GET)
	public ModelAndView questionDetail(@RequestParam("id") long id,
			@RequestParam("readCount") int readCount) {
		logger.info("问题详情页面");
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		boolean tag = false;
		if (user != null) {
			if (user.getRole().toString().equals(Role.ADMIN.toString())
					|| user.getRole().toString().equals(Role.SUPER.toString())) {
				tag = true;
			}
		}
		ModelAndView mvc = new ModelAndView();
		mvc.addObject("tag", tag);
		mvc.setViewName(ViewPaths.HELP_DETAIL);
		questionService.updataReadCount(id, readCount);
		return mvc;
	}

	/**
	 * 帮助中心
	 * 
	 * @return ModelAndView
	 */
	@RequestMapping(value = "/helpCenter", method = RequestMethod.GET)
	public ModelAndView helpCenter() {
		logger.info("问题详情页面");
		ModelAndView mvc = new ModelAndView();
		mvc.setViewName(ViewPaths.HELP_CENTER);
		return mvc;
	}

	/**
	 * 得到问题列表<br>
	 * 游客也可以查看
	 * 
	 * @param dataRequest
	 * @return
	 */
	@RequestMapping(value = "/loadQuestions", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse getQuestion(DataRequest dataRequest) {
		logger.info("得到问题");
		DataResponse dataResponse = new DataResponse();
		int curpage = dataRequest.getPage();
		int pageSize = dataRequest.getRows();
		Page page = questionService.getQuestions(curpage, pageSize);
		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());
		return dataResponse;
	}

	/**
	 * 根据问题ID得到回复列表<br>
	 * 游客也可以查看
	 * 
	 * @param dataRequest
	 * @return
	 */
	@RequestMapping(value = "/loadQuestionsByQuestionId", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse getQuestionByQuestionId(DataRequest dataRequest,
			long questionId) {
		logger.info("得到问题");
		DataResponse dataResponse = new DataResponse();
		int curpage = dataRequest.getPage();
		int pageSize = dataRequest.getRows();
		Page page = replyService.getRepliesByQuestionId(questionId, pageSize,
				curpage);
		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());
		return dataResponse;
	}

	/**
	 * 提问题
	 * 
	 * @param question
	 * @return
	 */
	@RequestMapping(value = "createQuestion", method = RequestMethod.POST)
	public @ResponseBody
	boolean createQuestion(Question question) {
		User user = WebHelper.getSessionUser();
		if (user == null) {
			return false;
		}
		long userId = user.getId();
		String userName = user.getNickname();
		question.setAskerId(userId);
		question.setAskerName(userName);
		question.setAskerPortrait(user.getPortrait());
		return questionService.saveQuestion(question);
	}

	/**
	 * 回答问题
	 * 
	 * @param reply
	 * @return
	 */
	@RequestMapping(value = "saveReply", method = RequestMethod.POST)
	public @ResponseBody
	String reply(Reply reply) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return "USER_NOT_LOGIN";
		}

		long userId = user.getId();
		String userName = user.getNickname();
		reply.setReplierId(userId);
		reply.setReplierName(userName);
		reply.setReplierPortrait(user.getPortrait());
		return replyService.save(reply) ? "true" : "false";
	}

	/**
	 * 删除问题
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "deleteQuestion", method = RequestMethod.POST)
	public @ResponseBody
	boolean deleteQuestionById(long id) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return false;
		}
		return questionService.deleteQuestion(id);
	}

	/**
	 * 删除回复
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "deleteReply", method = RequestMethod.POST)
	public @ResponseBody
	boolean deleteReplyById(long id) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return false;
		}
		return replyService.delete(id);
	}

	/**
	 * 更新问题
	 * 
	 * @param question
	 * @return
	 */
	@RequestMapping(value = "updateQuestion", method = RequestMethod.POST)
	public @ResponseBody
	boolean updateQuestion(Question question) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return false;
		}
		return questionService.updateQuestion(question);
	}

	/**
	 * 更新回复
	 * 
	 * @param question
	 * @return
	 */
	@RequestMapping(value = "updateReply", method = RequestMethod.POST)
	public @ResponseBody
	boolean updateReply(long id, String content) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return false;
		}
		return replyService.update(content, id);
	}

	/**
	 * 根据id得到答复的问题
	 * 
	 * @param dataRequest
	 * @param replyId
	 * @return
	 */

	@RequestMapping(value = "/loadQuestionsByReplyId", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse getQuestionByReplyId(DataRequest dataRequest, long replyId) {
		logger.info("得到回答的问题");
		DataResponse dataResponse = new DataResponse();
		int curpage = dataRequest.getPage();
		int pageSize = dataRequest.getRows();
		Page page = questionService.getQuestionsByApplIerId(replyId, curpage,
				pageSize);
		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());
		return dataResponse;
	}

	/**
	 * 根据id得到提过的问题
	 * 
	 * @param dataRequest
	 * @param askerId
	 * @return
	 */
	@RequestMapping(value = "/loadQuestionsByAskId", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse getQuestionByAskId(DataRequest dataRequest, long askerId) {
		logger.info("得到回答的问题");
		DataResponse dataResponse = new DataResponse();
		int curpage = dataRequest.getPage();
		int pageSize = dataRequest.getRows();
		Page page = questionService.getQuestionsByAskerId(askerId, curpage,
				pageSize);
		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());
		return dataResponse;
	}

	/**
	 * 帮助中心
	 * 
	 * @return ModelAndView
	 */
	@RequestMapping(value = "/searchDetail", method = RequestMethod.GET)
	public ModelAndView searchDetail(@RequestParam("id") long id) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);

		int messageCount = MessageRemainsHelper.getRemains(user.getId());

		ModelAndView mvc = new ModelAndView();
		mvc.setViewName(ViewPaths.SEARCHDETAIL);
		Question question = questionService.getQuestion(id);
		mvc.addObject("question", question);
		mvc.addObject("user", user);
		mvc.addObject("messageCount", messageCount);

		return mvc;
	}
}
